﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Sublimation
{
    public class DescriptionItem : GameObject
    {
        protected String text;
        protected SpriteFont font;
        protected Vector2 fontPos;
        protected Vector2 descUpPos;
        public Vector2 DescUpPos
        {
            get { return descUpPos; }
            set { descUpPos = value; }
        }
        protected Vector2 descDownPos;
        public Vector2 DescDownPos
        {
            get { return descDownPos; }
            set { descDownPos = value; }
        }

        public Vector2 FontPos
        {
            set { fontPos = value; }
            get { return fontPos; }
        }
        protected GameObject parent;
        public String Text
        {
            set { 
            text = this.WrapText(font,value,400f);
            }
            get { return text; }
        }

        public DescriptionItem(WorldProperties worldProperties,GameObject parent,String text,Vector2 position)
            :base(worldProperties)
        {
            this.Position = position;
            this.text = text;
            this.parent = parent;
            scale = 1f;

            Vector2 tempPos = new Vector2(Position.X, Position.Y);
            if (Rotation != 0f)
            {
                tempPos = RotatePoint(parent.Position, MathHelper.TwoPi - Rotation, tempPos);
            }
            descDownPos = new Vector2(tempPos.X - 15, tempPos.Y + 130);
            descDownPos = RotatePoint(parent.Position, Rotation, descDownPos);
            descUpPos = new Vector2(tempPos.X - 15, tempPos.Y);
            descUpPos = RotatePoint(parent.Position, Rotation, descUpPos);
            
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            Sprite = Game.Content.Load<Texture2D>("Sprites/SlotSelect/descriptionBorder");
            font = Game.Content.Load<SpriteFont>("Miramonte");
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            if (text != null)
            {
                spriteBatch.DrawString(font, Text,fontPos , Color.Black, rotation, Origin, 0.6f, SpriteEffects.None, 1f);
            }
            spriteBatch.End();
        }

        public void Translate(Vector2 translateVector)
        {
            Position += translateVector;
            fontPos += translateVector;
            descUpPos += translateVector;
            descDownPos += translateVector;
        }

        public void Rotate(float deltaRotation)
        {
            Position = RotatePoint(parent.Position, deltaRotation, Position);
            fontPos = RotatePoint(parent.Position, deltaRotation, fontPos);
            Rotation += deltaRotation;
            descUpPos = RotatePoint(parent.Position, deltaRotation, descUpPos);
            descDownPos = RotatePoint(parent.Position, deltaRotation, descDownPos);
        }

        public string WrapText(SpriteFont spriteFont, string text, float maxLineWidth)
        {
            string[] words = text.Split(' ');

            StringBuilder sb = new StringBuilder();

            float lineWidth = 0f;

            float spaceWidth = spriteFont.MeasureString(" ").X;

            foreach (string word in words)
            {
                Vector2 size = spriteFont.MeasureString(word);

                if (lineWidth + size.X < maxLineWidth)
                {
                    sb.Append(word + " ");
                    lineWidth += size.X + spaceWidth;
                }
                else
                {
                    sb.Append("\n" + word + " ");
                    lineWidth = size.X + spaceWidth;
                }
            }

            return sb.ToString();
        }

        public void SetLocationUp(bool up)
        {
            if (up)
            {
                position = descUpPos;

                Vector2 tempVector = new Vector2(position.X, position.Y);
                if (Rotation != 0f)
                {
                    tempVector = RotatePoint(parent.Position,MathHelper.TwoPi - rotation,tempVector);
                }
                
                fontPos = new Vector2(tempVector.X + 10, tempVector.Y + 10);
               
                if (Rotation != 0f)
                {
                    fontPos = RotatePoint(parent.Position, rotation, fontPos);
                }
               
            }
            else
            {
                position = descDownPos;

                Vector2 tempVector = new Vector2(descDownPos.X, descDownPos.Y);
                if (Rotation != 0f)
                {
                    tempVector = RotatePoint(parent.Position, MathHelper.TwoPi - rotation, tempVector);
                }

                fontPos = new Vector2(tempVector.X + 10, tempVector.Y + 10);

                if (Rotation != 0f)
                {
                    fontPos = RotatePoint(parent.Position, rotation, fontPos);
                }

            }
        }
    }
}
